app.config(function($routeProvider, $locationProvider) {

	$routeProvider.when('/home', {
		templateUrl: "home/index.html",
		controller: 'homeController'
	});

});

function homeService($http, $location) {
	return {
		list: function(limit, page, app_id, fn) {
			var param = {
				limit: limit,
				page: page
			};
			baas_yl.call({
				'http': $http,
				'method': 'get',
				'uri': 'order/orderList/list',
				'data': param,
				'success': function(d) {
					if (typeof fn == 'function') {
						fn(d);
					}
				}
			});
		}
        ,orderlist: function(data,limit, page, app_id, fn) {
            var param = {
                limit: limit,
                page: page,
                start_time:(data != ''&& data != undefined)?data.start_time:'',
                end_time:(data != ''&& data != undefined)?data.end_time:'',
                company:(data != ''&& data != undefined)?data.company:'',
                status:(data != ''&& data != undefined)?data.status:'',
                score:(data != ''&& data != undefined)?parseInt(data.score):'',
                serviceType1:(data != ''&& data != undefined && data.serviceType1!=undefined)?data.serviceType1:'',
                serviceType2:(data != ''&& data != undefined && data.serviceType2!=undefined)?data.serviceType2:'',
                //serviceType3:(data != ''&& data != undefined && data.serviceType3!=undefined)?data.serviceType3:'',
                order_id:(data != ''&& data != undefined)?data.order_id:'',
                name:(data != ''&& data != undefined)?data.username:'',
                address:(data != ''&& data != undefined)?data.address:'',
                phone:(data != ''&& data != undefined)?data.phone:''
            };
            if(data != ''&& data != undefined && data.serviceType3!=undefined)
                param.serviceType3 = data.serviceType3;
            baas_yl.call({
                'http': $http,
                'method': 'post',
                'uri': 'order/orderList/count',
                'data' : param,
                'success': function(d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
		,fucount : function(group,where_serviceType1,app_id,fn){
			var data = {
				group : 'serviceType2',
				where_serviceType1 : where_serviceType1
			};
			baas_yl.call({
				'http' : $http,
				'method' : 'get',
				'uri' : 'order/order/statistics',
				'data' : data,
				'success' : function(d){
					if (typeof fn == 'function') {
						fn(d);
					}
				}
			});
		}
		,yuecount : function(start_date,app_id,fn){
            var param = {
                start_date: start_date
            };
			baas_yl.call({
				'http' : $http,
				'method' : 'get',
                'data' : param,
				'uri' : 'order/order/month/count',
				'success' : function(d){
					if (typeof fn == 'function') {
						fn(d);
					}
				}
			});
		}
		,satisfied : function(group,where_serviceType1,app_id,fn){
			var data = {
				group : 'score',
				where_serviceType1 : where_serviceType1,
                where_status : '完成',
                score : 1
			};
			baas_yl.call({
				'http' : $http,
				'method' : 'get',
				'uri' : 'order/order/statistics',
				'data' : data,
				'success' : function(d){
					if (typeof fn == 'function') {
						fn(d);
					}
				}
			});
		}
		,laoCount : function(app_id,fn){
			baas_yl.call({
				'http' : $http,
				'method' : 'get',
				'uri' :'archives/archives/count',
				'success' : function(d){
					if (typeof fn == 'function') {
						fn(d);
					}
				}
			});
		}
        ,laoCountAll : function(app_id,fn){
            baas_yl.call({
                'http' : $http,
                'method' : 'get',
                'uri' :'archives/archives/list',
                'success' : function(d){
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }

        ,servantCount : function(data,fn){
            baas_yl.call({
                'http' : $http,
                'method' : 'get',
                'data' : data,
                'uri' : 'servant/servant/working/hours/statistics',
                'success' : function(d){
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
		,amountCount : function(data,app_id,fn){
			baas_yl.call({
				'http' : $http,
				'method' : 'get',
				'data' : data,
				'uri' : 'card/index/statistics/card/amount',
				'success' : function(d){
					if (typeof fn == 'function') {
						fn(d);
					}
				}
			});
		}
	};
}

app.factory('homeService', ['$http', '$location', homeService]);

app.controller('homeController', ['$scope', '$routeParams', '$window', '$location', '$http', 'homeService', homeController]);


function homeController($scope, $window, $location, $http, $routeParams, homeService) {
	//老人会员卡统计
	$scope.laoCount =function(){
		homeService.laoCountAll($scope.app_id,function(json){
			$scope.laolist = json.count;
			//console.log(json)
		});
	}
	$scope.laoCount();
    //收入统计
    $scope.amountCount = function(){
        var data = {};
        data.start_time = '2015-10-22 00:00:00';
        homeService.amountCount(data,$scope.app_id, function(json) {
            $scope.totalCount = json['total'];
        });
    }
    $scope.amountCount();
	//服务统计
	//$scope.group = 'serviceType2';
	$scope.where_serviceType1 = 'yanglao';
	$scope.fucount = function(){
		homeService.fucount($scope.group,$scope.where_serviceType1,$scope.app_id,function(json){
			$scope.listCount = json;
			//$scope.listCounts = json.res;
			var dataDonut = [];
            var other = 0;
			for(var i=0;i<json.res.retval.length;i++){
                //10% 一下统计为其他的项目
                if(parseFloat(json.res.retval[i].rate) > parseFloat("3%")){
                    var js = {label:json.res.retval[i].serviceType2_title,data:json.res.retval[i].count};
                    dataDonut.push(js);
                }else{
                    other += json.res.retval[i].count;
                }
			}
            dataDonut.push({label:'其他',data:other});

			fun(dataDonut);
		});
	};
	$scope.fucount();
	//订单统计
	$scope.yuecount = function(){
        var myDate = new Date();
        var year = myDate.getFullYear();
        var month = myDate.getMonth() + 1;
        var start_date = year+'-'+ month+'-'+'01';
		homeService.yuecount(start_date,$scope.app_id,function(json){
			$scope.listCounts = json;
//			console.log(json);
			var likes = new Array(json.res.retval.length);
			for (var i=0;i<likes.length;i++) {
				var time=new Date(json.res.retval[i].create_date);
				//console.log(time.getDate());
				likes[i] = [time.getDate(),json.res.retval[i].count];
			}
			order(likes);
			//console.log(json);
		});
	};
	$scope.yuecount();
	//满意度统计
	$scope.manyiRate = 0;
	$scope.satisfied = function(){
		homeService.satisfied($scope.group,$scope.where_serviceType1,$scope.app_id,function(json){
			$scope.salist = json.res.retval;
			for(  p in $scope.salist  ){
				if( $scope.salist[p].score == 5 || $scope.salist[p].score == 3){
					$scope.manyiRate = $scope.manyiRate + parseFloat($scope.salist[p].rate);
				}
			}
            $scope.manyiRate = $scope.manyiRate.toFixed(2) + '%';
		});
	};
	$scope.satisfied();
	
	$scope.selectedPage = 1;
	$scope.limit = 5;
	$scope.getList = function() {
        var data = {};
        data.serviceType1 = 'yanglao';
		homeService.orderlist(data,$scope.limit, $scope.selectedPage, $scope.app_id, function(json) {
			$scope.list = json['list'];
			$scope.orderCount = json['count'];
			$scope.currentPage = 1;
			$scope.getPage(json['count']);
			console.log(json['list']+"+123+");
		});
	};
	$scope.getList();

	/*分页*/
	$scope.changePage = function(p) {
		$scope.data.selectedPage = p;
		homeService.list($scope.limit, p, $scope.app_id, function(json) {
			$scope.list = json.list;
			$scope.count = json.count;
			$scope.currentPage = p;
			$scope.getPage(json.count);
		//	console.log($scope.currentPage)
		});
	};
	$scope.getPage = function(count) {
		$scope.pageList = [];
		var limit = 5; //返回的条数
		$scope.totalPage = parseInt(count / limit); //总页数
		$scope.totalPage += count % limit >= 1 ? 1 : 0; //判断页数是否是整数
		var start = ($scope.currentPage - 2) >= 1 ? $scope.currentPage - 2 : 1; //首页
		var end = ($scope.currentPage + 2) <= $scope.totalPage ? $scope.currentPage + 2 : $scope.totalPage; //尾页

		$scope.frontdotShow = start > 1 ? true : false; //前面的省略号
		$scope.backdotShow = end < $scope.totalPage ? true : false; //后面的省略号
		for (var i = start; i <= end; i++) {
			$scope.pageList.push({
				number: i,
				active: i == $scope.currentPage ? 'active' : ''
			}); //给点击的按钮添加class
//			console.log(count);
//			console.log(	$scope.totalPage );
		}
	};

	//从业人员 ，周，月，年 统计
	var weeks={week:1};
	homeService.servantCount(weeks,function(d){
		console.log(d.hours);
		servantGraph(d.hours,"graph-bar-weeks");
	});
	var months={month:1};
	homeService.servantCount(months,function(d){
		console.log(d);
		servantGraph(d.hours,"graph-bar-months");
	});
	var years={year:1};
	homeService.servantCount(years,function(d){
		console.log(d);
		servantGraph(d.hours,"graph-bar-year");
	});
}

















